from subprocess import run
from os import system
class AddUser():
    def addUser(self,name):
        addInspect = run(['useradd',name])
        if addInspect.returncode == 0:
            print("Add user {0}".format(name))
            return 1
        if addInspect.returncode == 9:
            print("user {0} already exists".format(name))
            return 0
        if addInspect.returncode == 3:
            print("{0} is not a valid user name".format(name))
            return 0
class A(AddUser):
    def checkShell(self,name):
        system("cat /etc/passwd | grep ^%s | awk -F: '{print $7}'" % (name))

    def checkUid(self,name):
        system("cat /etc/passwd | grep ^%s | awk -F: '{print $3}'" % (name))

    def checkGid(self,name):
        system("cat /etc/passwd | grep ^%s | awk -F: '{print $4}'" % (name))

    def modUid(self,name,uid):
        modUidInspect = run(['usermod','-u',uid,name])
        if modUidInspect.returncode == 4:
            print("UID {0} already exists".format(uid))
            return 0
        if modUidInspect.returncode == 8:
            print("user {0} is currently used".format(name))
            return 0
        if modUidInspect.returncode == 6:
            print("user {0} does not exist".format(name))
            return 0
        if modUidInspect.returncode == 3:
            print("{0} is not a valid uid".format(uid))
            return 0
        if modUidInspect.returncode == 0:
            print("Changed UID")
    def modGid(self,name,gid):
        modGidInspect = run(['usermod','-g',gid,name])
        if modGidInspect.returncode == 4:
            print("GID {0} already exists".format(gid))
            return 0
        if modGidInspect.returncode == 8:
            print("user {0} is currently used".format(name))
            return 0
        if modGidInspect.returncode == 6:
            print("user {0} does not exist".format(name))
            return 0
        if modGidInspect.returncode == 3:
            print("{0} is not a valid gid".format(gid))
            return 0
        if modGidInspect.returncode == 0:
            print("Changed GID")
            return 1
    def modShell(self,name,dir):
        modUidInspect = run(['usermod','-s',dir,name])
        if modUidInspect.returncode == 6:
            print("user {0} does not exist".format(name))
            return 0
        if modUidInspect.returncode == 0:
            print("Changed SHELL")
            return 1
        else:
            raise ValueError
class B(AddUser):
    def delUser(self,name):
        delUInspect = run(['userdel','-r',name])
        if delUInspect.returncode == 0:
            print("Remove user {0}".format(name))
            return 1
        if delUInspect.returncode == 8:
            print("user {0} is currently used".format(name))
            return 0
        if delUInspect.returncode == 6:
            print("user {0} does not exist".format(name))
            return 0
    def delGroup(self,name):
        delGInspect = run(['groupdel',name])
        if delGInspect.returncode == 0:
            print("Remove group {0}".format(name))
            return 1
        if delGInspect.returncode == 8:
            print("group {0} is currently used".format(name))
            return 0
        if delGInspect.returncode == 6:
            print("group {0} does not exist".format(name))
            return 0
class C(A,B):
    def addGroup(self,name):
        addGInspect = run(['groupadd',name])
        if addGInspect.returncode == 0:
            print("Add group {0}".format(name))
            return 1
        if addGInspect.returncode == 9:
            print("group {0} already exists".format(name))
            return 0
        if addGInspect.returncode == 3:
            print("{0} is not a valid group name".format(name))
            return 0
